import 'package:flutter/widgets.dart';
import 'package:my_flutter_tool/util/log_utils.dart';
import 'app_state.dart';
import 'user.dart';

import 'dart:math' as math;

///独立评论数据封装
class CommentModle with ChangeNotifier {
  CommentModle();

  @required
  notifyListeners() {
    if (_mounted) super.notifyListeners();
  }

  ///是否构建
  bool _mounted = false;

  ///楼层列表
  List<Floor> _floorList = List<Floor>();

  ///评论当前的状态
  ViewState _commentState = ViewState.ready;

  ///楼层总数量
  int _commentCount = 0;

  ///获取楼层列表
  List<Floor> get floorList => _floorList;

  ///获取楼层数量
  int get commrntCount => _commentCount;

  ///获取评论状态
  ViewState get commentState => _commentState;

  ///初始化数据
  ///网络请求可在此进行
  void init() async {
    _mounted = true;

    _commentCount = 40;

    final List<Reply> _tempReply = List<Reply>();
    for (int i = 0; i < math.Random().nextInt(4) + 1; i++) {
      _tempReply.add(
        Reply(
          createTime: DateTime.now(),
          replyContent: "回复😁回复回复回复回复回复回复回复回复回复回复回复回复回复回复",
          replyUser: User(name: '回复者$i'),
        ),
      );
    }

    for (int i = 0; i < 10; i++) {
      _floorList.add(Floor(
        floorId: i,
        isFloorLike: false,
        floorLikeCount: 0,
        floorUser: User(name: '测试人员'),
        floorContent: '内容😁内容内容内容内容内容内容内容',
        createFloorTime: DateTime.now(),
        replyCount: 20,
        replyList: _tempReply,
      ));
    }

    await Future.delayed(const Duration(seconds: 1), () {});

    _commentState = ViewState.normal;
    notifyListeners();
  }

  ///清理评论模块
  void clear() async {
    _mounted = false;
    _floorList.clear();
    _floorList.clear();
  }

  ///点赞和取消点赞
  ///* `floorIdIndex` 点赞楼层时传楼层索引
  ///* `replyIdIndex` 点赞回复时传回复索引
  Future<bool> like({int floorIdIndex, int replyIdIndex}) async {
    return true;
  }

  ///添加楼层(评论)
  Future<int> addFloor(String content, User user) async {
    _floorList.insert(
      0,
      Floor(
        floorId: 0,
        isFloorLike: false,
        floorLikeCount: 0,
        floorUser: user,
        floorContent: content,
        createFloorTime: DateTime.now(),
        replyCount: 0,
        replyList: [],
      ),
    );

    _commentCount++;

    notifyListeners();

    return 0;
  }

  ///回复
  ///* `floorIdIndex` 回复楼层时传楼层索引
  ///* `replyIdIndex` 回复回复时传回复索引
  Future<int> addReply(User user, String content,
      {int floorIdIndex, int replyIdIndex}) async {
    _floorList[floorIdIndex].replyList.add(Reply(
          replyContent: content,
          replyId: 0,
          replyLikeCount: 0,
          replyUser: user,
          isReplyLike: false,
        ));

    _floorList[floorIdIndex].replyCount++;

    notifyListeners();
    return 0;
  }

  ///加载更多楼层/评论
  Future<bool> loadMoreFloor() async {
    L.i('加载更多数据');

    final List<Floor> _temp = List.from(_floorList);

    final List<Reply> _tempReply = List<Reply>();
    for (int i = 0; i < math.Random().nextInt(4) + 1; i++) {
      _tempReply.add(
        Reply(
          createTime: DateTime.now(),
          replyContent: "回复😁回复回复回复回复回复回复回复回复回复回复回复回复回复回复",
          replyUser: User(name: '回复者$i'),
        ),
      );
    }

    for (var i = 0; i < 3; i++) {
      _temp.add(
        Floor(
          floorId: 0,
          isFloorLike: false,
          floorLikeCount: 0,
          floorUser: User(name: '测试人员'),
          floorContent: '内容😁内容内容内容内容内容内容内容',
          createFloorTime: DateTime.now(),
          replyCount: 20,
          replyList: _tempReply,
        ),
      );
    }

    _floorList = _temp;

    await Future.delayed(const Duration(seconds: 1), () {});

    notifyListeners();

    return true;
  }

  ///加载/展开更多回复
  Future<List<Reply>> loadMoreReply() async {
    return [];
  }
}

///评论
class Comment {
  ///id
  int id;

  ///用户
  User user;

  ///内容
  String content;

  ///点赞数
  int likeCount;

  ///我是否点赞
  bool isLike;

  ///创建时间
  DateTime createTime;

  Comment({
    this.id,
    this.user,
    this.content,
    this.likeCount,
    this.isLike,
    this.createTime,
  });
}

///楼层
class Floor extends Comment {
  ///回复列表
  List<Reply> replyList;

  ///回复总数
  int replyCount;

  ///是否展开回复
  bool isExtendReply;

  ///回复的状态
  ViewState replyState;

  Floor({
    ///楼层id
    int floorId = 0,

    ///楼主
    User floorUser,

    ///楼层内容
    String floorContent,

    ///楼层点赞数
    int floorLikeCount = 0,

    ///我是否点赞该楼层
    bool isFloorLike = false,

    ///创建日期
    DateTime createFloorTime,

    ///扩展参数
    this.replyCount = 0,
    this.replyList,
    this.isExtendReply = false,
    this.replyState = ViewState.normal,
  }) : super(
          id: floorId,
          user: floorUser,
          content: floorContent,
          likeCount: floorLikeCount,
          isLike: isFloorLike,
          createTime: createFloorTime,
        );
}

///回复
class Reply extends Comment {
  Reply({
    ///回复id
    int replyId = 0,

    ///回复者
    User replyUser,

    ///回复内容
    String replyContent,

    ///回复点赞数
    int replyLikeCount = 0,

    ///我是否点赞该回复
    bool isReplyLike = false,

    ///创建日期
    DateTime createTime,
  }) : super(
          id: replyId,
          user: replyUser,
          content: replyContent,
          likeCount: replyLikeCount,
          isLike: isReplyLike,
          createTime: createTime,
        );
}
